package demo.practice.dp;

public class P5 {
    public static void main(String[] args) {
        Solution s = new Solution();
        s.longestPalindrome("bb");
    }

    static class Solution {
        public String longestPalindrome(String s) {
            int maxlen = 1;
            int fl = 0;
            char[] cs = s.toCharArray();

            for (int i = 1; i < s.length(); i++) {
                //以当前位置为中心点
                int l = i - 1;
                int r = i + 1;
                int tm = 1;
                int tl = i;

                while (r < s.length() && cs[r] == cs[i]) {
                    r++;
                    tm++;
                }
                while (l >= 0 && cs[l] == cs[i]) {
                    l--;
                    tm++;
                    tl--;
                }
                while (l >= 0 && r < s.length() &&  cs[l] ==cs[r]) {
                    l--;
                    r++;
                    tl--;
                    tm += 2;
                }
                fl = tm > maxlen ? tl : fl;
                maxlen = Math.max(tm, maxlen);

            }
            return s.substring(fl, fl + maxlen);
        }
    }
}
